<template>
  <div class="myOrder">
	<PageContainer
    title="我的订单"
    :containerStyle="{
      background: '#F5F5F5'
    }" back no-tab>
    <AutofixScroll :height="height" @bottom="onBottom">
      <div class="order-list">
        <div v-for="(item, index) in list" :key="index" class="order-item" @click="goDetail(item)">
          <div class="img-container">
            <u-image :src="computedImage(item)" :width="imgWidth" :height="imgHeight" @click="click"></u-image>
          </div>
          <div class="order-info">
            <div class="title">{{ item.ordeName }}</div>
            <div class="price">
              <template v-if="item.orderType === 1">
                实付金额：￥{{ item.total }}
              </template>
              <template v-if="item.orderType === 2">
                实付积分：{{ item.totalPoint }} 积分
              </template>
            </div>
            <div class="status">订单状态：{{ item.status }}</div>
          </div>
        </div>
      </div>
      <u-loadmore :status="loadStatus" loadmoreText="加载更多" nomoreText="没有更多了" />
    </AutofixScroll>
	</PageContainer>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import AutofixScroll from '@/components/AutofixScroll/index';
import PageContainer from '@/components/PageContainer/index.vue'
import {useRouter} from '@/hooks/useRouter';
import { orderList } from '@/api'

const router = useRouter();

const list = ref([])
const loadStatus = ref('loading')
const imgWidth = uni.upx2px(208) + 'px'
const imgHeight = uni.upx2px(246) + 'px'
const height = '100%'

const goDetail = (item) => {
  router.push({
    path: `/pages/orderDetail/index?orderCode=${item.orderCode}&orderType=${item.orderType}`,
  });
}

const computedImage = (item) => item.customerInfo?.project_info?.[0]?.imageList?.[0] || ''

const params = {
  pageNo: 1,
  pageSize: 10,
}
let total = 0
const getOrderList = async () => {
  loadStatus.value = 'loading'
  try {
    const res = await orderList(params)
    list.value = [...list.value, ...(res.records || [])]
    total = res.total
    if (list.value.length === total || total === 0) {
      loadStatus.value = 'nomore'
    } else {
      loadStatus.value = 'loadmore'
    }
  } catch(error) {
    loadStatus.value = 'nomore'
  }
}

const onBottom = () => {
  if (list.value.length >= total) {
    loadStatus.value = 'nomore'
    return
  }
  params.pageNo += 1
  getOrderList()
}

onMounted(() => {
  getOrderList()
})

</script>

<style lang="less" scoped>
.myOrder {
	.order-list {
    padding: 8px;
    .order-item {
      width: 100%;
      padding: 8px;
      box-sizing: border-box;
      background: #fff;
      display: flex;
      &:not(:last-child) {
        margin-bottom: 8px;
      }

      .order-info {
        .title {
          font-size: 16px;
          font-weight: 400;
          color: #282828;
          line-height: 24px;
          margin-bottom: 10px;
          overflow:hidden;
          text-overflow:ellipsis;
          display:-webkit-box;
          -webkit-box-orient:vertical;
          -webkit-line-clamp:2;
        }

        .price {
          font-size: 14px;
          margin-bottom: 10px;
        }

        .status {
          font-size: 14px;
        }
      }

      .img-container {
        width: 104px;
        height: 123px;
        flex-shrink: 0;
        margin-right: 16px;
        .img {
          width: 100%;
          height: 100%;
          object-fit: cover;
        }
      }
    }
  }
}
</style>
